package com.ketai.dao.impl;

import java.util.Date;

import com.ketai.dao.ActivityDao;
import com.ketai.dao.BaseDao;
import com.ketai.pojo.CActivity;
import com.ketai.vo.ActivityVo;

import javax.naming.NamingException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:长沙科泰梦想学院-蓝色小药丸
 * @Mission:打造演说·创客型跨境电商与互联网人才
 * @Description:
 * @Date:Create In 2022/8/4 21:57
 **/
public class ActivityDaoImpl extends BaseDao implements ActivityDao {
    @Override
    public List<ActivityVo> getAllActivities (String sql, Object[] params) throws SQLException, NamingException {

        ResultSet rs = query(sql, params);
        List<ActivityVo> activities = new ArrayList<ActivityVo>();
        while (rs.next()) {
            ActivityVo activity = new ActivityVo();
            activity.setId(rs.getString("id"));
            activity.setTitle(rs.getString("title"));
            activity.setActionTime(rs.getDate("time"));
            activity.setPbName(rs.getString("PbName"));
            activity.setTypeName(rs.getString("typeName"));
            activity.setPosition(rs.getString("position"));
            activity.setApply_time(rs.getDate("apply_time"));
            activity.setApply_end_time(rs.getDate("apply_end_time"));
            activity.setApply_people_num(rs.getInt("apply_people_num"));
            activity.setChecked(rs.getInt("checked"));
            activity.setCheckedName(rs.getString("checkedName"));
            activity.setChecked_time(rs.getDate("checked_time"));
            activities.add(activity);
        }

        return activities;
    }

    @Override
    public int addActivity (CActivity activity) throws SQLException, NamingException {

        String sql="INSERT INTO `researchtrip`.`c_activity`(`id`, `pbid`, `title`, `tid`, `time`, `position`,`img`, `detail`," +
                " `apply_time`, `apply_end_time`, `apply_people_num`)" +
                " VALUES (?,?,?,?,?,?,?,?,?,?,?)";
           Object [] params={activity.getId(), activity.getPbid(),activity.getTitle(), activity.getTid(), activity.getTime(), activity.getPosition(), activity.getImg(), activity.getDetail(),activity.getApply_time(),activity.getApply_end_time(),activity.getApply_people_num()}  ;
        return curdMethod(sql,params);
    }

    @Override
    public int deleteActivityById (String id) throws SQLException, NamingException {
        String sql="delete  from c_activity where id = "+id;
       int result= curdMethod(sql,null);
        return result;
    }

    @Override
    public int editActivity (CActivity activity) {
        return 0;
    }

    @Override
    public long getTotalActivityCount (String sql, Object[] params) throws SQLException, NamingException {
        ResultSet rs = query(sql, params);
        while(rs.next()) {
            return rs.getLong("total");
        }

        return 0;
    }

    @Override
    public ActivityVo getActivityById (String id) throws SQLException, NamingException {
        String  sql="select b.*,u.name checkedName from (select  a.*,o.name pbName,t.name  typeName from c_activity a ,  organizationbase  o , activity_type t where a.tid=t.id and a.pbid=o.id and a.id=?) b  left join  background_user u on u.id= checkedId";
         Object [] params = {id};
        ResultSet rs = query(sql,params);
        ActivityVo activity = new ActivityVo();
        while (rs.next()) {

            activity.setId(rs.getString("id"));
            activity.setTitle(rs.getString("title"));
            activity.setActionTime(rs.getDate("time"));
            activity.setPbName(rs.getString("PbName"));
            activity.setTypeName(rs.getString("typeName"));
            activity.setPosition(rs.getString("position"));
            activity.setApply_time(rs.getDate("apply_time"));
            activity.setApply_end_time(rs.getDate("apply_end_time"));
            activity.setApply_people_num(rs.getInt("apply_people_num"));
            activity.setChecked(rs.getInt("checked"));
            activity.setCheckedName(rs.getString("checkedName"));
            activity.setChecked_time(rs.getDate("checked_time"));
            activity.setImg(rs.getString("img"));
            activity.setDetail(rs.getString("detail"));
        }
        return activity;
    }

    @Override
    public int checkedActity (String sql, Object[] params) throws SQLException, NamingException {
         int result=curdMethod(sql, params);
         return result;
    }
}
